package com.letao.service;

import java.util.List;

import com.letao.dao.AdminDAO;
import com.letao.dao.UserDAO;
import com.letao.dao.UserFocusDAO;
import com.letao.dao.UserexpandDAO;
import com.letao.pojo.Admin;
import com.letao.pojo.User;
import com.letao.pojo.UserFocus;
import com.letao.pojo.Userexpand;

public class UserService implements UserServiceInterface {
	private UserDAO userDao;
	private UserexpandDAO userExpandDao;
	

	private AdminDAO adminDao;
	private UserFocusDAO userFocusDao;
	@Override
	public boolean addUser(User user) throws Exception {
		// TODO Auto-generated method stub
		userDao.save(user);
		return true;
	}

	@Override
	public boolean deleteUser(User user) throws Exception {
		// TODO Auto-generated method stub
		userDao.delete(user);
		return true;
	}

	@Override
	public boolean updateUser(User user) throws Exception {
		// TODO Auto-generated method stub
		userDao.attachDirty(user);
		return true;
	}
	

	@Override
	public boolean addAdmin(Admin admin) throws Exception {
		// TODO Auto-generated method stub
		adminDao.save(admin);
		return true;
	}

	@Override
	public boolean deleteAdmin(Admin admin) throws Exception {
		// TODO Auto-generated method stub
		adminDao.delete(admin);
		return true;
	}

	@Override
	public boolean updateAdmin(Admin admin) throws Exception {
		// TODO Auto-generated method stub
		adminDao.attachDirty(admin);
		return true;
	}

	@Override
	public User checkUserByUalaisAndPass(String Ualais, String Upassword)
			throws Exception {
		// TODO Auto-generated method stub
		List<User> userList=userDao.findByUalais(Ualais);
		if(userList.size()==0) return null; 
		User user=(User) userList.get(0);
		if(user.getUpassword().equals(Upassword)) return user;
		return user;
	}

	@Override
	public boolean checkUserByUalais(String Ualais) throws Exception {
		// TODO Auto-generated method stub
		
		if(userDao.findByUalais(Ualais).size()>0) return false;
		return true;
	}

	@Override
	public List<User> findAllUser() throws Exception {
		// TODO Auto-generated method stub
		
		return userDao.findAll();
	}

	@Override
	public List<Admin> findAllAdmin() throws Exception {
		// TODO Auto-generated method stub
		return adminDao.findAll();
	}

	@Override
	public boolean addUserFocus(UserFocus userFocus) throws Exception {
		// TODO Auto-generated method stub
		userFocusDao.save(userFocus);
		return true;
	}

	@Override
	public boolean deleteUserFocus(UserFocus userFocus) throws Exception {
		// TODO Auto-generated method stub
		userFocusDao.delete(userFocus);
		return true;
	}

	@Override
	public boolean updateUserFocus(UserFocus userFocus) throws Exception {
		// TODO Auto-generated method stub
		userFocusDao.attachDirty(userFocus);
		return true;
	}

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

	

	public AdminDAO getAdminDao() {
		return adminDao;
	}

	public void setAdminDao(AdminDAO adminDao) {
		this.adminDao = adminDao;
	}

	@Override
	public User getUserByUid(Integer Uid) throws Exception {
		// TODO Auto-generated method stub
		return userDao.findById(Uid);
	}

	@Override
	public boolean addUserExpand(Userexpand userExpand) throws Exception {
		// TODO Auto-generated method stub
		userExpandDao.save(userExpand);
		return true;
	}

	@Override
	public boolean deleteUserExpand(Userexpand userExpand) throws Exception {
		// TODO Auto-generated method stub
		userExpandDao.delete(userExpand);
		return false;
	}

	@Override
	public boolean updateUserExpand(Userexpand userExpand) throws Exception {
		// TODO Auto-generated method stub
		userExpandDao.attachDirty(userExpand);
		return true;
	}

	@Override
	public Userexpand getUserExpandByUid(Integer Uid) throws Exception {
		// TODO Auto-generated method stub
		return userExpandDao.findById(Uid);
	}
	@Override
	public List<UserFocus> getUserFocusByUser(Integer Uid) throws Exception {
		// TODO Auto-generated method stub
		return userFocusDao.findByProperty("userByUid.uid", Uid);
	}
	public UserFocusDAO getUserFocusDao() {
		return userFocusDao;
	}

	public void setUserFocusDao(UserFocusDAO userFocusDao) {
		this.userFocusDao = userFocusDao;
	}

	public UserexpandDAO getUserExpandDao() {
		return userExpandDao;
	}

	public void setUserExpandDao(UserexpandDAO userExpandDao) {
		this.userExpandDao = userExpandDao;
	}

}
